package com.example.listviewtest;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import com.example.listViewTest.R;

public class MainActivity extends AppCompatActivity {

    private List<Cat> catList = new ArrayList<Cat>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initCats(); // 初始化猫数据
        CatAdapter adapter = new CatAdapter(MainActivity.this, R.layout.cat_item, catList);
        ListView listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Cat cat = catList.get(position);
                Toast.makeText(MainActivity.this, cat.getName(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initCats() {

        Cat cat1= new Cat("cat1", R.drawable.cat1);
            catList.add(cat1);
        Cat cat2= new Cat("cat2", R.drawable.cat2);
            catList.add(cat2);
        Cat cat3= new Cat("cat3", R.drawable.cat3);
            catList.add(cat3);
        Cat cat4= new Cat("cat4", R.drawable.cat4);
            catList.add(cat4);
        Cat cat5= new Cat("cat5", R.drawable.cat5);
            catList.add(cat5);



    }

}